# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=libgedit-amtk
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=5.9.1
pkgrel=1
pkgdesc="Actions, Menus and Toolbars Kit for GTK applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://gedit-technology.github.io/'
msys2_repository_url='https://gitlab.gnome.org/World/gedit/libgedit-amtk'
license=('spdx:LGPL-3.0-or-later')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-gettext-tools"
  "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
  "${MINGW_PACKAGE_PREFIX}-gtk-doc"
  "${MINGW_PACKAGE_PREFIX}-meson"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
)
depends=(
  "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
  "${MINGW_PACKAGE_PREFIX}-glib2"
  "${MINGW_PACKAGE_PREFIX}-gtk3"
)
conflicts=("${MINGW_PACKAGE_PREFIX}-amtk")
source=("https://gitlab.gnome.org/World/gedit/libgedit-amtk/-/archive/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('185c960789b7b448f3c6b7d0e15da5785906930a2bb5fddb45dfc0bd3d1b0798')

build() {
  local -a _static_flags=(
    -DGIO_STATIC_COMPILATION
    -DGLIB_STATIC_COMPILATION
    -DGMODULE_STATIC_COMPILATION
    -DGOBJECT_STATIC_COMPILATION
  )

  local -a _meson_options=(
    --prefix="${MINGW_PREFIX}"
    --wrap-mode=nodownload
    --auto-features=enabled
    --buildtype=plain
  )

  CFLAGS+=" ${_static_flags[@]}" \
  CXXFLAGS+=" ${_static_flags[@]}" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    "${_meson_options[@]}" \
    --default-library=static \
    -Dgtk_doc=false \
    -Dgobject_introspection=false \
    "${_realname}-${pkgver}" \
    "build-${MSYSTEM}-static"

  ${MINGW_PREFIX}/bin/meson.exe compile -C "build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    "${_meson_options[@]}" \
    --default-library=shared \
    -Dgtk_doc=true \
    -Dgobject_introspection=true \
    "${_realname}-${pkgver}" \
    "build-${MSYSTEM}-shared"

  ${MINGW_PREFIX}/bin/meson.exe compile -C "build-${MSYSTEM}-shared"
}

check() {
  ${MINGW_PREFIX}/bin/meson.exe test -C "build-${MSYSTEM}-static" || warning "Tests failed"
  ${MINGW_PREFIX}/bin/meson.exe test -C "build-${MSYSTEM}-shared" || warning "Tests failed"
}

package_libgedit-amtk() {
  conflicts=("${MINGW_PACKAGE_PREFIX}-amtk")
  replaces=("${MINGW_PACKAGE_PREFIX}-amtk")

  ${MINGW_PREFIX}/bin/meson.exe install -C "build-${MSYSTEM}-static" --destdir "${pkgdir}"
  ${MINGW_PREFIX}/bin/meson.exe install -C "build-${MSYSTEM}-shared" --destdir "${pkgdir}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSES/LGPL-3.0-or-later.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # split docs
  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/gtk-doc dest${MINGW_PREFIX}/share/gtk-doc
}

package_libgedit-amtk-docs() {
  pkgdesc+=" (documentation)"
  depends=()
  conflicts=()
  replaces=()

  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
